package com.bnz.manager.controller;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

//import org.apache.shiro.authz.annotation.RequiresPermissions;
import com.bnz.manager.entity.group.Specification;
import com.bnz.manager.service.SpecificationOptionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.bnz.manager.entity.SpecificationEntity;
import com.bnz.manager.service.SpecificationService;
import com.bnz.common.PageUtils;
import com.bnz.common.entity.R;


/**
 * 
 *
 * @author wangye
 * @email 1149688570@qq.com
 * @date 2023-03-23 19:22:25
 */
@RestController
@RequestMapping("mapper/specification")
public class SpecificationController {
    // 引入规格接口
    @Autowired
    private SpecificationService specificationService;
    // 引入规格选项接口
    @Autowired
    private SpecificationOptionService optionService;

    /**
     * 列表
     */
    @GetMapping("/list")
    //@RequiresPermissions("mapper:specification:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = specificationService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @GetMapping("/info/{id}")
    //@RequiresPermissions("mapper:specification:info")
    public R info(@PathVariable("id") String id){
		// SpecificationEntity specification = specificationService.getById(id);
        // 点击修改按钮时要先查询出规格的相关信息
        Specification specification = specificationService.findById(id);
        return R.ok().put("specification", specification);
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    //@RequiresPermissions("mapper:specification:save")
    public R save(@RequestBody Specification specification){
		specificationService.insert(specification);

        return R.ok();
    }

    /**
     * 修改
     */
    @PutMapping("/update")
    //@RequiresPermissions("mapper:specification:update")
    public R update(@RequestBody Specification specification){
		specificationService.updateSpec(specification);

        return R.ok();
    }

    /**
     * 删除
     */
    @DeleteMapping("/delete")
    //@RequiresPermissions("mapper:specification:delete")
    public R delete(@RequestBody String[] ids){
		specificationService.delete(Arrays.asList(ids));

        return R.ok();
    }

    // 查询所有规格
    @GetMapping("/findAll")
    public R findAll(){
        // 将specname换成text
        List<Map> collect = specificationService.list().stream().map(m -> {
            Map map = new HashMap();
            map.put("id", m.getId());
            map.put("text", m.getSpecName());
            return map;
        }).collect(Collectors.toList());
        return R.ok().put("list",collect);
    }
}
